package com.sphd.beautysalon.model;


import com.android.volley.VolleyError;
import com.sphd.beautysalon.presenter.FoodMenuPresenterImpl;
import com.sphd.beautysalon.info.GreensInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/16
 */

public class FoodMenuModelImpl implements FoodMenuModel, VolleyJsonObjectCallBack {
    FoodMenuPresenterImpl foodMenuPresenter;

    @Override
    public void sendUrl(String url, FoodMenuPresenterImpl foodMenuPresenter) {
        this.foodMenuPresenter = foodMenuPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void sendDelUrl(String s, FoodMenuPresenterImpl foodMenuPresenter) {
        this.foodMenuPresenter = foodMenuPresenter;
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res =response.getInt("res");
                    foodMenuPresenter.sendRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            foodMenuPresenter.sendError(error);
            }
        });

    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<GreensInfo> greensInfos = JsonUtil.getListFromString(response.getString("res"), GreensInfo.class);
            foodMenuPresenter.sendRes(greensInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        foodMenuPresenter.sendError(error);
    }
}